﻿using System.Collections.Generic;

namespace YXYLCommon
{    
    public abstract class BaseDAL
    {
        protected string tableName=null;
        protected string viewName = null;
        protected string primaryColumn = null;
        protected string nameColumn = null;
        protected string parentColumn = null;
        protected DbProcObject dbProc = new DbProcObject();

        public virtual List<T> GetAll<T>() where T :BaseModel,new()
        {
            dbProc.ProcName = "select * from " + viewName;
            return CommonService.Ini().GetListBySql<T>(dbProc);
        }

        public virtual List<T> GetPage<T>(BasePageInfo pageinfo) where T : BaseModel, new()
        {
            dbProc.ProcName = "p_"+tableName+"GetPage";
            pageinfo.FillPageInfo(dbProc);
            return CommonService.Ini().GetPage<T>(pageinfo);
        }

        public virtual List<T> GetByParent<T>(int parentValue) where T : BaseModel, new()
        {
            dbProc.ProcName = "select * from " + viewName + " where " + this.parentColumn + "=@ParentValue";
            dbProc.ProcParameters.Clear();
            dbProc.AddParameter("@ParentValue", parentValue);
            return CommonService.Ini().GetListBySql<T>(dbProc);
        }

        public virtual T GetByID<T>(int id) where T : BaseModel, new()
        {
            dbProc.ProcName = "select * from " + viewName + " where " + this.primaryColumn + "=@ObjID";
            dbProc.ProcParameters.Clear();
            dbProc.AddParameter("@ObjID", id);
            return CommonService.Ini().GetObjectBySql<T>(dbProc); ;
        }
        public virtual T GetByID<T>(string id) where T : BaseModel, new()
        {
            dbProc.ProcName = "select * from " + viewName + " where " + this.primaryColumn + "=@ObjID";
            dbProc.ProcParameters.Clear();
            dbProc.AddParameter("@ObjID", id);
            return CommonService.Ini().GetObjectBySql<T>(dbProc); ;
        }

        public virtual T GetByName<T>(string name) where T : BaseModel, new()
        {
            dbProc.ProcName = "select * from " + tableName + " where " +this.nameColumn + "=@ObjName";
            dbProc.ProcParameters.Clear();
            dbProc.AddParameter("@ObjName", name);
            return CommonService.Ini().GetObjectBySql<T>(dbProc); ;
        }

        public virtual DbCmdResult DeleteById(int id)
        {
            dbProc.ProcParameters.Clear();
            dbProc.ProcName = "p_" + tableName+"DelById";
            dbProc.AddParameter("@ObjID", id);
            return CommonService.Ini().ExcuteProc(dbProc);
        }

        public virtual DbCmdResult Save(BaseModel model)
        {
            dbProc.ProcParameters.Clear();
            dbProc.ProcName = "p_" + tableName + "Save";
            fillSaveProc(model);
            return CommonService.Ini().ExcuteProc(dbProc);
        }
        protected abstract void fillSaveProc(BaseModel model);
    }
}
